namespace two {
    export let sceneLayer: eui.UILayer;

    export function getStageWith() {
        return sceneLayer.stage.stageWidth;
    }

    export function getStageHeight() {
        return sceneLayer.stage.stageHeight;
    }

    export function initScene(uiRoot: eui.UILayer): void {
        sceneLayer = new eui.UILayer();
        uiRoot.addChild(sceneLayer);
        sceneLayer.touchEnabled = false;

        uiRoot.stage.addEventListener(egret.Event.RESIZE, onResize, null);
    }

    function onResize(): void {
        let height = sceneLayer.stage.stageHeight;

        for (let i = 0; i < scales.length; i++) {
            changeResize(scales[i].group, scales[i].minHeight, scales[i].maxHeight, scales[i].minProps, scales[i].maxProps, sceneLayer.stage.stageHeight);
        }
    }

    function changeResize(group: egret.DisplayObject, minHeight: number, maxHeight: number, minInfo: { [key: string]: number }, maxInfo: { [key: string]: number }, height: number): void {
        if (maxHeight > height) {//有缩放
            for (let key in maxInfo) {
                group[key] = (maxInfo[key] - minInfo[key]) * (height - minHeight) / (maxHeight - minHeight) + minInfo[key];
            }
        }
        else {
            for (let key in maxInfo) {
                group[key] = maxInfo[key];
            }
        }
    }

    var scales: { group: egret.DisplayObject, minHeight: number, maxHeight: number, minProps: { [key: string]: number }, maxProps: { [key: string]: number } }[] = [];

    export function addResizeMonitor(group: egret.DisplayObject, minHeight: number, maxHeight: number, minProps: { [key: string]: number }, maxProps: { [key: string]: number }): void {
        scales.push({ group: group, minHeight: minHeight, maxHeight: maxHeight, minProps: minProps, maxProps: maxProps });
        changeResize(group, minHeight, maxHeight, minProps, maxProps, sceneLayer.stage.stageHeight);
    }

    export function removeResizeMonitor(group: egret.DisplayObject): void {
        for (let i = 0; i < scales.length; i++) {
            if (scales[i].group == group) {
                scales.splice(i, 1);
                return;
            }
        }
    }


    let typeClasses: { [type: string]: { clazz: string } } = {};
    export function setClassMap(type: string, clazz: string): void {
        typeClasses[type] = { clazz: clazz };
    }

    export function getClassObj(classType: string): any {
        if (typeClasses[classType]) {
            let info = typeClasses[classType];
            let cl = egret.getDefinitionByName(info.clazz);
            return new cl();;
        }
    }

    export let currentPageType: string;

    let currentPage: eui.Component;
    export function changePage(name: string, type: number = 0): eui.Component {
        currentPageType = name;

        if (sceneLayer.numChildren > 0) {
            if (type == 0) {
                sceneLayer.removeChildren();
            }
            else {
                var c = fixedLayer(sceneLayer.stage, type, sceneLayer.stage.stageWidth, sceneLayer.stage.stageHeight);
                sceneLayer.removeChildren();
                sceneLayer.addChild(c);
            }
        }
        currentPage = getClassObj(name);
        if (currentPage) {
            currentPage.touchEnabled = false;
            currentPage.percentWidth = currentPage.percentHeight = 100;
            sceneLayer.addChildAt(currentPage, 0);
        }
        return currentPage;
    }

    export function fixedLayer(layer: egret.DisplayObject, type: number, w: number, h: number): egret.DisplayObjectContainer {
        var loadTxGrp = new egret.DisplayObjectContainer();

        // var w = sceneLayer.stage.stageWidth;
        // var h = sceneLayer.stage.stageHeight;
        let sp = getSpriteSheet(layer, w, h);

        //循环创建多个截图bitmap 这里num自由设置
        var tx1Number = 40;
        //每个横着的数量
        var Xnumber = 5;
        //高数量自动计算
        var Ynumber = tx1Number / Xnumber;
        for (var i = 0; i < tx1Number; i++) {
            //计算每个的XY及宽高
            var _mcW = w / Xnumber;
            var _mcH = h / Ynumber;
            var _mcX = i % Xnumber * _mcW;
            var _mcY = Math.floor(i / Xnumber) * _mcH;

            var bmp = new egret.Bitmap;
            bmp.texture = sp.createTexture("mc_" + i, _mcX, _mcY, _mcW, _mcH);
            bmp.anchorOffsetX = _mcW / 2
            bmp.anchorOffsetY = _mcH / 2
            bmp.x = _mcX + _mcW / 2;
            bmp.y = _mcY + _mcH / 2;
            loadTxGrp.addChild(bmp);
            var tw = egret.Tween.get(bmp);
            //开始特效
            switch (type) {
                case 1:
                    tw.to({ scaleX: 0, scaleY: 0, alpha: 0, rotation: 359 }, 800, egret.Ease.circIn);
                    break;
                case 2:
                    var my_x = -w
                    if (!(i % 2)) {
                        my_x = w * 2;
                    }
                    tw.to({ x: my_x, alpha: 0 }, 800, egret.Ease.circIn);
                    break;
                case 3:
                    tw.to({ scaleX: 0.2, scaleY: 1, alpha: 0, blurFliter: 0 }, 800, egret.Ease.backInOut);
                    break;
                case 4:
                    tw.to({ alpha: 0 }, 900, egret.Ease.circIn);
                    break;
                default:
                    tw.to({ scaleX: 1, scaleY: 0, alpha: 0 }, 800, egret.Ease.circIn);
            }

            if (i == tx1Number - 1) {
                tw.call(onComplete, this);
            }
        }

        sceneLayer.touchChildren = false;
        function onComplete() {
            sceneLayer.touchChildren = true;
            if (loadTxGrp && loadTxGrp.parent) {
                loadTxGrp.parent.removeChild(loadTxGrp);
            }
        }

        return loadTxGrp;
    }

    var tempTexture: egret.RenderTexture;
    function getSpriteSheet(layer: egret.DisplayObject, w: number, h: number): SpriteSheet {
        if (tempTexture == null) {
            tempTexture = new egret.RenderTexture();
        }
        tempTexture.drawToTexture(layer, new egret.Rectangle(0, 0, w, h));
        var spriteSheet = new SpriteSheet(tempTexture);
        return spriteSheet;
    }

    class SpriteSheet {
        public constructor(texture: egret.Texture) {
            this.$texture = texture;
        }

        private _bitmapX: number = 0;
        private _bitmapY: number = 0;
        private $texture: egret.Texture;
        public createTexture(name: string, bitmapX: number, bitmapY: number, bitmapWidth: number, bitmapHeight: number, offsetX: number = 0, offsetY: number = 0, textureWidth?: number, textureHeight?: number): egret.Texture {
            if (textureWidth === void 0) {
                textureWidth = offsetX + bitmapWidth;
            }
            if (textureHeight === void 0) {
                textureHeight = offsetY + bitmapHeight;
            }

            if (egret.Capabilities.renderMode == "webgl") {
                bitmapY = this.$texture.textureHeight - bitmapY - bitmapHeight;
            }

            let texture: egret.Texture = new egret.Texture();
            // texture.disposeBitmapData = false;
            texture.bitmapData = this.$texture.bitmapData;
            texture.$initData(this._bitmapX + bitmapX, this._bitmapY + bitmapY, bitmapWidth, bitmapHeight, offsetX, offsetY, textureWidth, textureHeight, this.$texture.textureWidth, this.$texture.textureHeight);
            return texture;
        }

        public dispose(): void {
            if (this.$texture) {
                this.$texture.dispose();
            }
        }
    }
}


